﻿using System.Collections;
using Actors;
using Common;
using GameCamera;
using GameScene;
using Net;
using UI;
using UI.Panels;

namespace GameStates
{
    public abstract class GameState : State
    {
        public static readonly GameInitializeState Initialize =  new GameInitializeState();
        public static readonly GameLoopState Loop = new GameLoopState();
    }
    
    public class GameInitializeState : GameState
    {
        public override void OnEnter()
        {
            GameRoot.CoroutineRoot.StartCoroutine(BeginInitialize());
        }

        public override void OnExit()
        {
            
        }
        
        private IEnumerator BeginInitialize()
        {
            yield return GameRoot.CoroutineRoot.StartCoroutine(GameInitialize());
            yield return GameRoot.CoroutineRoot.StartCoroutine(PostInitialize());
        }
        
        private IEnumerator GameInitialize()
        {
            UIManager.Instance.Init();
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
//             var homePanel = UIManager.Instance.OpenPanel("HomePanel", UILayer.LayerIndex_UI, 50) as HomePanel;
// #if UNITY_EDITOR
//             if(homePanel == null)
//             {
//                 UnityEngine.Debug.LogError("HomePanel is null");
//             }
// #endif
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
            
            GameSceneManager.Instance.Initialize();
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
            GameSceneManager.Instance.LoadScene("Demo");
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
            CameraManager.Instance.Init();
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
            NetWorkManager.Instance.StartConnect();
            yield return CoroutineRoot.defaultWaitForEndOfFrame;
            
        }
        
        /// <summary>
        /// 需要待其他模块初始化完成后再初始化的内容
        /// </summary>
        private IEnumerator PostInitialize()
        {
            GameSceneManager.Instance.PostInitialize();
            GameSceneManager.Instance.SpawnLocalPlayer(new ActorInitParams());
            CameraManager.Instance.SetFollowTarget(GameSceneManager.Instance.LocalPlayer.transform, GameSceneManager.Instance.LocalPlayer.transform);
            yield return CoroutineRoot.defaultWaitForEndOfFrame;

            GameRoot.Instance.ChangeState(GameState.Loop);
        }
    }
    
    public class GameLoopState : GameState
    {
        public override void OnEnter()
        {

        }

        public override void OnExit()
        {
            NetWorkManager.Instance.CloseConnect();
        }
        
        public override void Update()
        {
            UnityMainThreadDispatcher.Instance.Update();
            DelaySecondCall.Update();
            DelayFrameCall.Update();
        
            GameSceneManager.Instance.Update();
            NetWorkManager.Instance.Update();
        }
        
        public override void FixedUpdate()
        {
            GameSceneManager.Instance.FixedUpdate();
            CameraManager.Instance.FixedUpdate();
        }
    }
}